<template>
	<view class="container">
		<view class="all" @tap.stop="signIn()">
			<view class="name">
				{{text}}
			</view>
			<view class="time">
				{{time}}
			</view>
		</view>
	</view>
</template>

<script>
	import api from '@/comon/api.js'
	export default {
		data(){
			return {
				time: '',
				text: '签到',
				timeStemp: ''
			}
		},
		onLoad() {
			uni.showLoading({})
		},
		onShow() {
			this.loadData();
		},
		methods:{
			async loadData(){
				let userinfo = uni.getStorageSync('userInfos');
				let res = await api.uniCloudApi(api.publicFun.sign_in_user,{_id:userinfo._id,type:0});
				if(res.msg){
					this.text = '已签到'
					this.timeStemp = res.time;
					this.returnTime();
				}else{
					this.returnTime();
				}
			},
			async signIn(){
				let userinfo = uni.getStorageSync('userInfos');
				let that = this;
				if(this.timeStemp == ''){
					let res =await api.uniCloudApi(api.publicFun.sign_in_user,{_id:userinfo._id,type: 1})
					console.log(res)
					if(res.updated){
						uni.showToast({
							icon:"none",
							title:'签到成功'
						})
						setTimeout(()=> {
							that.loadData();
						},1000)
					}
				}
			},
			returnTime(){
				let that = this;
				if(that.timeStemp != ''){
					let data = new Date(that.timeStemp);
					let year = data.getFullYear();
					let month = that.returnLength(data.getMonth() + 1);
					let day = that.returnLength(data.getDate());
					let hour = that.returnLength(data.getHours());
					let min = that.returnLength(data.getMinutes());
					let ss = that.returnLength(data.getSeconds());
					let str = `${year}-${month}-${day} ${hour}:${min}:${ss}`
					that.time = str;
				}else {
					setInterval(()=>{
						let data = new Date();
						let year = data.getFullYear();
						let month = that.returnLength(data.getMonth() + 1);
						let day = that.returnLength(data.getDate());
						let hour = that.returnLength(data.getHours());
						let min = that.returnLength(data.getMinutes());
						let ss = that.returnLength(data.getSeconds());
						let str = `${year}-${month}-${day} ${hour}:${min}:${ss}`
						that.time = str;
					},1000);
				}
				uni.hideLoading();
			},
			returnLength(e){
				e = `${e}`
				if (e.length == 1) {
					e = '0' + e
				}
				return e
			}
		}
	}
</script>

<style lang="scss">
	.container{
		width: 100%;
	}
	.all{
		width: 350rpx;
		height: 350rpx;
		background-color: $uni-back-color;
		border-radius: 175rpx;
		margin-left: calc((100% - 350rpx)/2);
		margin-top: 150rpx;
		color: #fff;
		.name{
			width: 100%;
			text-align: center;
			height: 100rpx;
			line-height: 100rpx;
			font-size: 60rpx;
			padding-top: 75rpx;
		}
		.time{
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
		}
	}
</style>
